package com.gs.core.web.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gs.core.domain.Audit;
import com.gs.core.service.AuditService;

@Controller
@RequestMapping("/repository/rest")
public class AuditRestController {
	
	@Autowired
	AuditService auditService;
	
	@RequestMapping("/audits")
	public String getAudits(Model model){		
		model.addAttribute("audits",auditService.findAll());
		return "audits";
	}
	@RequestMapping(value="/newAudit",method=RequestMethod.GET)
	public ModelAndView getNewForm(){
		return new ModelAndView("audit","command",new  Audit());
	}
	
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public String add(Audit audit){
		auditService.saveOrUpdate(audit);
		return "redirect:audits";
	}
	@RequestMapping(value="/save",method=RequestMethod.PUT)
	public String edit(Audit audit){
		auditService.saveOrUpdate(audit);
		return "redirect:audits";
	}
	
	@RequestMapping(value="/audit/{id}",method=RequestMethod.GET)
	@ResponseBody public Audit audit(@PathVariable("id") Long id){
		return auditService.findById(id);
	}
	
}
